package com.app.micaihu.ad.toutiao;

import com.app.micaihu.ad.AdNativeRequest;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;


/**
 * 头条 的 request
 */
public class ToutiaoNativeRequest extends AdNativeRequest<TTNativeExpressAd> {

    private TTAdNative mTTAdNative;
    private AdSlot adSlot;

    public ToutiaoNativeRequest(int retryNum, String adPosId, LinkedList<TTNativeExpressAd> nrAdList, AdSlot adSlot) {
        super(retryNum, adPosId, nrAdList);
        this.adSlot = adSlot;
    }

    @Override
    protected void reqeustAd() {
        if (mTTAdNative == null) {
            TTAdManager ttAdManager = TTAdManagerHolder.get();
            mTTAdNative = ttAdManager.createAdNative(Utils.getApp().getApplicationContext());
        }

//        HashMap<String, String> map = AdMapUtils.getPositionMap();
//        map.put(AdMapUtils.adPositionList + AdMapUtils.adRequestKey, adPosId);
//        MobclickAgentUtils.event(Config.EventId.toutiao_ad, map);

        mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
//                HashMap<String, String> map = AdMapUtils.getPositionMap();
//                map.put(AdMapUtils.adPositionList + "onError", adPosId);
//                map.put(AdMapUtils.adPositionList + "errorMsg", message);
//                map.put(AdMapUtils.adPositionList + "errorCode", code + "");
//                MobclickAgentUtils.event(Config.EventId.toutiao_ad, map);
                LogUtils.e("toutiao", "id->" + adPosId + " ToutiaoNativeRequest code->" + code + " message->" + message);
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                LogUtils.e("List<TTNativeExpressAd>", list.size());
//                HashMap<String, String> map = AdMapUtils.getPositionMap();
//                map.put(AdMapUtils.adPositionList + AdMapUtils.adSuccessKey, adPosId);
//                MobclickAgentUtils.event(Config.EventId.toutiao_ad, map);
                if (list != null) {
                    int size = list.size();
                    if (size > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            TTNativeExpressAd ttNativeExpressAd = list.get(i);
                            if (ttNativeExpressAd != null) ttNativeExpressAd.render();
                        }
                        nrAdList.addAll(list);
                    }
                }
//                ToutiaoNativeAd.removeRetryCache(hashCode);
            }
        });

    }

}
